package card_discount

import (
	sqlc "bp/db/sqlc"
	sharedConv "bp/internal/rpcserver/shared/conv"
	"bp/internal/service/param"
	cardDiscountGrpc "bp/rpc/gen/webapi/card_discount"
)

type CardDiscountPtrEntityToPbFunc func(*sqlc.CardDiscount) *cardDiscountGrpc.CardDiscountEntity
type CardDiscountPtrArrayEntityToPbFunc func([]*sqlc.CardDiscount) []*cardDiscountGrpc.CardDiscountEntity

// ConvCardDiscountPtrEntityToPb get func of *sqlc.CardDiscount => *cardDiscountGrpc.CardDiscountEntity
func (s *CardDiscountWebApiImp) ConvCardDiscountPtrEntityToPb(
	itemCategoryPtrs param.ItemCategoryConfigurationPtrArray,
	itemPtrs param.ItemConfigurationPtrArray,
) CardDiscountPtrEntityToPbFunc {
	return func(ptr *sqlc.CardDiscount) *cardDiscountGrpc.CardDiscountEntity {
		if ptr == nil {
			return nil
		}
		return &cardDiscountGrpc.CardDiscountEntity{
			Id:                      ptr.ID,
			OwnerType:               ptr.OwnerType,
			OwnerId:                 ptr.OwnerID,
			OutCardId:               ptr.OutCardID,
			Name:                    ptr.Name,
			Desc:                    ptr.Desc.String,
			Tags:                    ptr.Tags,
			OfferCategory:           ptr.OfferCategory,
			OfferType:               ptr.OfferType,
			Discount:                ptr.Discount,
			IsPaused:                ptr.IsPaused,
			NeedVerifyQualification: ptr.NeedVerifyQualification,
			QualifyTypes:            ptr.QualifyTypes,
			UseTimeRanges:           sharedConv.ConvTimeRangeArrayEntityToPb(ptr.UseTimeRanges),
			DateChooseType:          ptr.DateChooseType.String,
			Weekdays:                ptr.Weekdays,
			Monthdays:               ptr.Monthdays,
			UseScenes:               ptr.UseScenes,
			Scope:                   ptr.Scope,
			ItemCategoryConfs:       sharedConv.ConvItemCategoryConfigurationPtrArrayEntityToPb(itemCategoryPtrs),
			ItemConfs:               sharedConv.ConvItemConfigurationPtrArrayEntityToPb(itemPtrs),
		}
	}
}

// ConvCardDiscountPtrArrayEntityToPb get func of []*sqlc.CardDiscount => []*cardDiscountGrpc.CardDiscountEntity
func (s *CardDiscountWebApiImp) ConvCardDiscountPtrArrayEntityToPb(
	itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap,
	itemMap param.EntityIDToItemConfigurationPtrsMap,
) CardDiscountPtrArrayEntityToPbFunc {
	return func(ptrs []*sqlc.CardDiscount) (vPtrs []*cardDiscountGrpc.CardDiscountEntity) {
		var itemCategoryPtrs param.ItemCategoryConfigurationPtrArray
		var itemPtrs param.ItemConfigurationPtrArray
		for _, ptr := range ptrs {
			if itemCategoryMap != nil {
				itemCategoryPtrs = itemCategoryMap[ptr.ID]
			}
			if itemMap != nil {
				itemPtrs = itemMap[ptr.ID]
			}
			vPtrs = append(vPtrs, s.ConvCardDiscountPtrEntityToPb(itemCategoryPtrs, itemPtrs)(ptr))
		}
		return
	}
}

type CardDiscountWithStatPtrEntityToPbFunc func(*sqlc.ListCardDiscountWithStatsRow) *cardDiscountGrpc.CardDiscountWithStatEntity
type CardDiscountWithStatPtrArrayEntityToPbFunc func([]*sqlc.ListCardDiscountWithStatsRow) []*cardDiscountGrpc.CardDiscountWithStatEntity

// ConvCardDiscountWithStatPtrEntityToPb get func of *sqlc.ListCardDiscountWithStatsRow => *cardDiscountGrpc.CardDiscountWithStatEntity
func (s *CardDiscountWebApiImp) ConvCardDiscountWithStatPtrEntityToPb(
	itemCategoryPtrs param.ItemCategoryConfigurationPtrArray,
	itemPtrs param.ItemConfigurationPtrArray,
) CardDiscountWithStatPtrEntityToPbFunc {
	return func(ptr *sqlc.ListCardDiscountWithStatsRow) *cardDiscountGrpc.CardDiscountWithStatEntity {
		if ptr == nil {
			return nil
		}
		return &cardDiscountGrpc.CardDiscountWithStatEntity{
			Self: &cardDiscountGrpc.CardDiscountEntity{
				Id:                      ptr.ID,
				OwnerType:               ptr.OwnerType,
				OwnerId:                 ptr.OwnerID,
				OutCardId:               ptr.OutCardID,
				Name:                    ptr.Name,
				Desc:                    ptr.Desc.String,
				Tags:                    ptr.Tags,
				OfferCategory:           ptr.OfferCategory,
				OfferType:               ptr.OfferType,
				Discount:                ptr.Discount,
				IsPaused:                ptr.IsPaused,
				NeedVerifyQualification: ptr.NeedVerifyQualification,
				QualifyTypes:            ptr.QualifyTypes,
				UseTimeRanges:           sharedConv.ConvTimeRangeArrayEntityToPb(ptr.UseTimeRanges),
				DateChooseType:          ptr.DateChooseType.String,
				Weekdays:                ptr.Weekdays,
				Monthdays:               ptr.Monthdays,
				UseScenes:               ptr.UseScenes,
				Scope:                   ptr.Scope,
				ItemCategoryConfs:       sharedConv.ConvItemCategoryConfigurationPtrArrayEntityToPb(itemCategoryPtrs),
				ItemConfs:               sharedConv.ConvItemConfigurationPtrArrayEntityToPb(itemPtrs),
			},
			Stat: &cardDiscountGrpc.CardDiscountStatEntity{
				OffTransCnt:       ptr.OffTransCnt,
				OffAmount:         ptr.OffAmount,
				OffRefundTransCnt: ptr.OffRefundTransCnt,
				OffRefundAmount:   ptr.OffRefundAmount,
			},
		}
	}
}

// ConvCardDiscountWithStatPtrArrayEntityToPb get func of []*sqlc.ListCardDiscountWithStatsRow => []*cardDiscountGrpc.CardDiscountWithStatEntity
func (s *CardDiscountWebApiImp) ConvCardDiscountWithStatPtrArrayEntityToPb(
	itemCategoryMap param.EntityIDToItemCategoryConfigurationPtrsMap,
	itemMap param.EntityIDToItemConfigurationPtrsMap,
) CardDiscountWithStatPtrArrayEntityToPbFunc {
	return func(ptrs []*sqlc.ListCardDiscountWithStatsRow) (vPtrs []*cardDiscountGrpc.CardDiscountWithStatEntity) {
		var itemCategoryPtrs param.ItemCategoryConfigurationPtrArray
		var itemPtrs param.ItemConfigurationPtrArray
		for _, ptr := range ptrs {
			if itemCategoryMap != nil {
				itemCategoryPtrs = itemCategoryMap[ptr.ID]
			}
			if itemMap != nil {
				itemPtrs = itemMap[ptr.ID]
			}
			vPtrs = append(vPtrs, s.ConvCardDiscountWithStatPtrEntityToPb(itemCategoryPtrs, itemPtrs)(ptr))
		}
		return
	}
}
